package adminorg;

import java.util.ArrayList;
import java.util.Collection;
import java.util.TreeMap;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import adminorg.DatabaseHelper;
import com.vaadin.data.util.filter.Compare.Equal;

public class OrgSchema extends VerticalLayout{
	/**
	 * 
	 */
	private static final long serialVersionUID = -6480762435583896034L;
	private int Company=0;
	private TreeMap <Integer, VerticalLayout> ML = new TreeMap <Integer, VerticalLayout>();
	private TreeMap <Integer, HorizontalLayout> HZ = new TreeMap <Integer, HorizontalLayout>();
	private TreeMap <Integer, Collection<?>> IDc = new TreeMap <Integer, Collection<?>>();
	
	private DatabaseHelper dbHelp = null;
	public TreeMap <Integer, OrgItem> ITEMS = new TreeMap <Integer, OrgItem>();
	
	private void setCompany(int Company){
		this.Company=Company;
	}
	
	private void setVBlockParam(Panel V){
		V.setWidth("100%");
		//if (V.getWidth()<130){
		//	V.setWidth("130px");
		//}
	}
	
	private VerticalLayout DrawRoot (VerticalLayout Parent,int Root){
		try
		{
		dbHelp.setCompanyId(this.Company);
		dbHelp.views.getOrgschemaViewContainer().removeAllContainerFilters();
		dbHelp.views.getOrgschemaViewContainer().addContainerFilter(new Equal("orgschema.id",Root));
		Object ID = dbHelp.views.getOrgschemaViewContainer().firstItemId();
		if (ID==null) return null;
		ITEMS.put(Root, new OrgItem(ID,Root,
	    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.pid").toString()),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.code").toString(),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.name").toString(),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.dept_name").toString(),
	    		 Boolean.parseBoolean(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.manager").toString()),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.docs").toString(),
	    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.level").toString()),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.name").toString(),
	    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "compacc.name").toString()
	    		 )); 				
		ML.put(Root, new VerticalLayout());
		setVBlockParam(ITEMS.get(Root));
		ML.get(Root).setWidth("100%");
		ML.get(Root).setSpacing(true);
		ML.get(Root).addComponent(ITEMS.get(Root));
		ML.get(Root).setComponentAlignment(ITEMS.get(Root), Alignment.TOP_CENTER);
		//HZ.get(Root).addStyleName("border");
		Parent.addComponent(ML.get(Root));
		}
		catch(Exception e)
		{
			
		}
		return ML.get(Root);
	}
	
	private void DrawByRoot(VerticalLayout Parent,int Root){
		if (Parent==null) return;
		try
		{
		    dbHelp.setCompanyId(this.Company);
			HZ.put(Root, new HorizontalLayout());
			HZ.get(Root).setWidth("100%");
			HZ.get(Root).setSpacing(true);
			ArrayList<Object> ItemIds = new ArrayList<Object>(dbHelp.views.getOrgschemaViewContainerByPar(Root).getItemIds());
			IDc.put(Root, ItemIds);
			for (Object ID:IDc.get(Root)){
				if (ID==null) return;
				int DrawId = Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID,"orgschema.id").toString());
				ITEMS.put(DrawId, new OrgItem(ID,DrawId,
			    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.pid").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.code").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.name").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.dept_name").toString(),
			    		 Boolean.parseBoolean(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.manager").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.docs").toString(),
			    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.level").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.name").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "compacc.name").toString()
			    		 )); 				
				ML.put(DrawId, new VerticalLayout());
				setVBlockParam(ITEMS.get(DrawId));
				ML.get(DrawId).setWidth("100%");
				ML.get(DrawId).setSpacing(true);
				ML.get(DrawId).addComponent(ITEMS.get(DrawId));
				ML.get(DrawId).setComponentAlignment(ITEMS.get(DrawId), Alignment.TOP_CENTER);
				HZ.get(Root).addComponent(ML.get(DrawId));
				//HZ.get(Root).addStyleName("border");
				Parent.addComponent(HZ.get(Root));
				DrawByRoot(ML.get(DrawId),DrawId);
			}
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			return;
		}
		
	}
	
	/*private void DrawByParentRoot(VerticalLayout Parent,int Root){
		if (Parent==null) return;
		try {
			VerticalLayout parent = Parent;
			int root = Root;
			dbHelp.setCompanyId(this.Company);		
			HZ.put(Root, new HorizontalLayout());
			HZ.get(Root).setWidth("100%");
			HZ.get(Root).setSpacing(true);
			for (Object ID:dbHelp.views.getOrgschemaViewContainer().getItemIds()){
				if (ID==null) return;
				int DrawId = Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID,"orgschema.id").toString());
				ITEMS.put(DrawId, new OrgItem(ID,DrawId,
			    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.pid").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.code").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.name").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.dept_name").toString(),
			    		 Boolean.parseBoolean(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.manager").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orgschema.docs").toString(),
			    		 Integer.parseInt(dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.level").toString()),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "orglevel.name").toString(),
			    		 dbHelp.views.getOrgschemaViewContainer().getContainerProperty(ID, "compacc.name").toString()
			    		 )); 				
				ML.put(DrawId, new VerticalLayout());
				setVBlockParam(ITEMS.get(DrawId));
				ML.get(DrawId).setWidth("100%");
				ML.get(DrawId).setSpacing(true);
				ML.get(DrawId).addComponent(ITEMS.get(DrawId));
				ML.get(DrawId).setComponentAlignment(ITEMS.get(DrawId), Alignment.TOP_CENTER);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}*/
	

	public OrgSchema(int Company,int Root,DatabaseHelper dbHelper){
		this();
		this.dbHelp = dbHelper;
		this.setSizeFull();
		setCompany(Company);
		if (Root==0){ 
		DrawByRoot(this,Root);
		} else {
			DrawByRoot(DrawRoot(this,Root),Root);
		}
	}

	public OrgSchema() {
		// TODO Auto-generated constructor stub
	}
	

}
